package com.nytimes.ohos.store.sample.data.model;

import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

/**
 * Immutable implementation of {@link Data}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutableData.builder()}.
 */
@SuppressWarnings("all")
public final class ImmutableData extends Data {
  private final ImmutableList<Children> children;

  private ImmutableData(ImmutableList<Children> children) {
    this.children = children;
  }

  /**
   * children
   *
   * @return The value of the {@code children} attribute
   */
  @Override
  public ImmutableList<Children> children() {
    return children;
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link Data#children() children}.
   *
   * @param elements The elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableData withChildren(Children... elements) {
    ImmutableList<Children> newValue = ImmutableList.copyOf(elements);
    return new ImmutableData(newValue);
  }

  /**
   * Copy the current immutable object with elements that replace the content of {@link Data#children() children}.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   *
   * @param elements An iterable of children elements to set
   * @return A modified copy of {@code this} object
   */
  public final ImmutableData withChildren(Iterable<? extends Children> elements) {
    if (this.children == elements) return this;
    ImmutableList<Children> newValue = ImmutableList.copyOf(elements);
    return new ImmutableData(newValue);
  }

  /**
   * This instance is equal to all instances of {@code ImmutableData} that have equal attribute values.
   *
   * @param another object
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(Object another) {
    if (this == another) return true;
    return another instanceof ImmutableData
        && equalTo((ImmutableData) another);
  }

  private boolean equalTo(ImmutableData another) {
    return children.equals(another.children);
  }

  /**
   * Computes a hash code from attributes: {@code children}.
   *
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 31;
    h = h * 17 + children.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code Data} with attribute values.
   *
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return MoreObjects.toStringHelper("Data")
        .omitNullValues()
        .add("children", children)
        .toString();
  }

  /**
   * Creates an immutable copy of a {@link Data} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   *
   * @param instance The instance to copy
   * @return A copied immutable Data instance
   */
  public static ImmutableData copyOf(Data instance) {
    if (instance instanceof ImmutableData) {
      return (ImmutableData) instance;
    }
    return ImmutableData.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutableData ImmutableData}.
   *
   * @return A new ImmutableData builder
   */
  public static Builder builder() {
    return new Builder();
  }

  /**
   * Builds instances of type {@link ImmutableData ImmutableData}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  public static final class Builder {
    private ImmutableList.Builder<Children> children = ImmutableList.builder();

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code Data} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     * Collection elements and entries will be added, not replaced.
     *
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(Data instance) {
      Preconditions.checkNotNull(instance, "instance");
      addAllChildren(instance.children());
      return this;
    }

    /**
     * Adds one element to {@link Data#children() children} list.
     *
     * @param element A children element
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addChildren(Children element) {
      this.children.add(element);
      return this;
    }

    /**
     * Adds elements to {@link Data#children() children} list.
     *
     * @param elements An array of children elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addChildren(Children... elements) {
      this.children.add(elements);
      return this;
    }

    /**
     * Sets or replaces all elements for {@link Data#children() children} list.
     *
     * @param elements An iterable of children elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder children(Iterable<? extends Children> elements) {
      this.children = ImmutableList.builder();
      return addAllChildren(elements);
    }

    /**
     * Adds elements to {@link Data#children() children} list.
     *
     * @param elements An iterable of children elements
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder addAllChildren(Iterable<? extends Children> elements) {
      this.children.addAll(elements);
      return this;
    }

    /**
     * Builds a new {@link ImmutableData ImmutableData}.
     *
     * @return An immutable instance of Data
     * @throws IllegalStateException if any required attributes are missing
     */
    public ImmutableData build() {
      return new ImmutableData(children.build());
    }
  }
}
